﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A62String类
{
    class Program
    {
        //字符串格式化
        static void Main1(string[] args)
        {
            string name = "张三";
            int age = 5;
            Console.WriteLine(DateTime.Now);
            //Console.WriteLine("{0}今年{1}岁",name,age);
            //Console.WriteLine($"{name}今年{age}岁");

            string str = string.Format("{0}今年{1}岁", name, age.ToString("00"));
            Console.WriteLine(str);
            string str1 = string.Format($"{name}今年{age:00}岁");
            string str3 = string.Format("今天是{0:yyyy}年{0:MM}月{0:dd}日  当前时间:{0:hh}点{0:mm}分{0:ss}秒", DateTime.Now);
            Console.WriteLine(str3);

            int hp = 89;
            int maxHp = 800;
            float percent = hp / (float)maxHp;

            //一下两行代码效果一样
            Console.WriteLine("当前血量{0:p}", percent);
            Console.WriteLine("当前血量{0}", percent.ToString("p"));
        }
        //判断字符串是否位空
        static void Main2(string[] args)
        {
            string s = " ";
            Console.WriteLine(string.IsNullOrEmpty(s));//false
            Console.WriteLine(string.IsNullOrWhiteSpace(s));//true
        }
        //合并字符串
        static void Main(string[] args)
        {
            string[] arr = { "A", "B", "C" };
            string str = string.Concat(arr[0], arr[1], arr[2]);
            Console.WriteLine(str);

            string str1 = string.Join("|", arr[0], arr[1], arr[2]);
            Console.WriteLine(str1);
        }

        //-----------------------------------------------------------字符串对象的方法-----------------------------------------------------------
        static void Main4(string[] args)
        {
            string str = "[AA-BB-CC-dd]";
            string str1 = str.Insert(0, "XX");
            Console.WriteLine(str1);

            //看似修改字符串的方法,都不会真的修改原字符串而是返回一个新的字符串

            //StartsWith();//是否以...开始
            Console.WriteLine(str.StartsWith("AA"));

            //EndsWith(); //是否以...结尾
            //Remove(); //移除
            string str2= str.Remove(1,2);
            Console.WriteLine(str2);

            //Substring();//截取
            string str3= str.Substring(0,2);
            Console.WriteLine(str3);
      
            //Replace(); //替换
            var str4=  str.Replace("BB","XX");
            Console.WriteLine(str4);

            //Split();//分割
            string[] arr= str.Split('-');
            foreach (var item in arr)
            {
                Console.WriteLine(item);
            }

            //ToLower(); //转小写
            string arr5  =str.ToUpper();
            Console.WriteLine(arr5);

            //ToUpper();//转大写

            //Trim();//默认删除首尾空格 可以用参数指定要删除的首尾字符
            string str6= str.Trim('[',']');
            Console.WriteLine(str6);

            //TrimStart();//删除首空格
            //TrimEnd();//删除尾空格

            //ToCharArray();//转字符数组
           char[] array= str.ToCharArray();
            foreach (char c in array)
            {
                Console.WriteLine(c); 
            }
        }

        //-----------------------------------------------------------字符串的不可变性----------------------------------------------------------
        static void Main9(string[] args)
        {
            //因为字符串高频率使用,很容易出现使用相同字符串的情况,所以为了节省内存只要是相同字符串都公用一个字符串对象,所有的字符串放在字符串池中
            string s = "你好";
            string s1 = "你好";
            Console.WriteLine(s.GetHashCode());
            Console.WriteLine(s1.GetHashCode());

            Dog d = new Dog();
            Dog d1 = new Dog();
            Console.WriteLine(d.GetHashCode());
            Console.WriteLine(d1.GetHashCode());


            //如果要多次修改字符串的内容,建议转cha数组操作
            char[]arr =  s.ToCharArray();
            arr[0]='我';
            string str = new string(arr);
            Console.WriteLine(str);
        }
    }

    class Dog
    {

    }
}
